<?php

/**
 * Data management abstract class
 *
 */
class DataMng
{
	/**
	 * MySQLDB class object
	 *
	 * @var MySQLDB
	 */
	private	$db;
	/**
	 * Unique identifier value
	 *
	 * @var integer
	 */
	private $id;
	/**
	 * Unique identifier keystring
	 *
	 * @var string
	 */
	private $idkey;

	/**
	 * Table name to manipulate data
	 *
	 * @var string
	 */
	private $table;

	/**
	 * MySQL tables prefix
	 *
	 * @var string
	 */
	public $prefix;

	/**
	 * DataMng class constructor
	 *
	 * @param integer $id
	 * @param string $idkey
	 * @return DataMng
	 */
	function DataMng($id = 0, $idkey = '')
	{
		$this->db = new MySQLDB();
		$this->prefix = $this->db->db_config['prefix'];

		if($id!=0)
		$this->id=$id;
		if($idkey!='')
		$this->idkey=$idkey;
	}

	/**
	 * Sets table name needed for data manipulation
	 *
	 * @param string $table
	 */
	function setWorkTable($table)
	{
		$this->table = $table;
	}

	/**
	 * Adds data to selected row
	 * associated array $data[keystring] = keyvalue;
	 *
	 * @param array $data
	 */
	function addData($data = array())
	{
		return $this->db->ExecuteNonQuery("INSERT INTO $this->table VALUES ('".implode('\',\'',$data)."');");
	}

	/**
	 * Edits selected row
	 * associated array $data[keystring] = keyvalue;
	 *
	 * @param array $data
	 */
	function editData($data = array())
	{
		$keys=array_keys($data);
		$query = "UPDATE $this->table SET ";
		$count=sizeof($data);
		for($i=0; $i<$count; $i++)
		{
			$query.=$keys[$i].'=\''.$data[$keys[$i]].'\'';
			if($i!=$count-1)
			$query.=',';
		}
		$query .= " WHERE $this->idkey='$this->id';";

		//print($query);

		return $this->db->ExecuteNonQuery($query);
	}

	/**
	 * Drops selected row from needed table
	 *
	 * @param string $idkey
	 * @param mixed $id
	 * @param string $table
	 * @return MySQL result
	 */
	function dropData($idkey='', $id='', $table='')
	{
		if($idkey=='' || $id=='')
		{
			$idkey = $this->idkey;
			$id = $this->id;
		}
		
		if($table=='')
		{
			$table=$this->table;
		}
	
		return $this->db->ExecuteNonQuery("DELETE FROM $table WHERE $idkey = '$id';");
	}

	/**
	 * Sets rowid
	 *
	 * @param integer $id
	 * @param string $idkey
	 */	function setId($id, $idkey)	
	{
		$this->id = $id;
		$this->idkey = $idkey;
	}

	/**
	 * Begins data reading from selected table
	 *
	 * @param array $keys
	 */
	function BeginDataRead($rows = '*', $limitation = false)
	{
		$rows = ($rows == '*' ? '*' : @implode(',',$rows));
		$this->db->ExecuteReader("SELECT $rows FROM $this->table WHERE $this->idkey = '$this->id' ".($limitation ? 'LIMIT '.(is_array($limitation) ? $limitation[0].','.$limitation[1] : $limitation) : '').";");
	}

	/**
	 * MySQLDB::ExecuteReader() interface
	 *
	 * @param string $query
	 */
	function BeginRawDataRead($query)
	{
		$this->db->ExecuteReader($query);
	}

	/**
	 * MySQLDB::Read() interface
	 *
	 * @return array
	 */
	function Read()
	{
		return $this->db->Read();
	}

	/**
	 * MySQLDB::escape() interface
	 *
	 * @param string $string
	 * @return string
	 */
	function escape($string)
	{
		return $this->db->escape($string);
	}

	/**
	 * Returns current table next auto_increment value
	 *
	 * @return string
	 */
	function GetTableAINextValue()
	{
		$result = mysql_query("SHOW TABLE STATUS FROM `".$this->db->db_config['db']."` LIKE '$this->table'");
		$row    = mysql_fetch_assoc($result);
		return $row['Auto_increment'];
	}
	
	/**
	 * Returns MySQLDB::last_query_num
	 *
	 * @return integer
	 */
	function GetQueries()
	{
		return $this->db->last_query_num;
	}
}
?>